package com.lagou.k8s.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：Swagger服务注册类
 *
 * @author lwb
 * @date 2019/3/15 10:52
 */
@EnableSwagger2
@Slf4j
@Configuration
public class SwaggerConfiguration implements WebMvcConfigurer {


    @Bean
    public Docket crateRestApi(){

        //header中的token参数非必填，传空也可以
        //根据每个方法名也知道当前方法在设置什么参数
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        ticketPar.name("x-pm-token").description("user token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(ticketPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(selector())
                //.paths(PathSelectors.any())
                //过滤错误接口不显示
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                //过滤监控接口不显示
                .paths(Predicates.not(PathSelectors.regex("/actuator.*")))
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("k8s")
                .description("k8s测试")
                .version("1.0")
                .build();
    }

    private Predicate<RequestHandler> selector(){
        return input ->{
            Class<?> declaringClass = input.declaringClass();
            return declaringClass.isAnnotationPresent(RestController.class) || input.isAnnotatedWith(ResponseBody.class);
        };
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }



}
